<?php

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$this->render('index');
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
	
	public function actionExcel()
	{
		$sheetArray = Yii::app()->yexcel->readActiveSheet('D:\andehuangbjcjwz\doc\test.xlsx');
		
		echo "<table>";
		
		foreach($sheetArray as $row)
		{
			echo "<tr>";
			foreach($row as $column)
				echo "<td>$column</td>";
			echo "</tr>";
		}
		
		echo "</table>";
	}

	//AutoComplete 实现
    public function actionAutoCompleteLookup()
    {
       if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
       {
	        $name = (string)$_GET['q'];//传的数据
	        
	        $criteriaz = new CDbCriteria;
	        $criteriaz->condition = "pinyin LIKE :sterm";
	        $criteriaz->params = array(":sterm"=>"%$name%");
	        
	        $data = array();
			$wzindex = WuzikuIndex::model()->findAll($criteriaz);
			foreach ($wzindex as $wuzi)
				$data[] = $wuzi->belongId;
			if ($data){
				$data = array_unique($data);//去重	
				$indexs = trim(implode(",",$data),',');
			}
          
	        // this was set with the "max" attribute of the CAutoComplete widget
	        $limit = min($_GET['limit'], 50);
	        $criteria = new CDbCriteria;
	        $criteria->condition = "id IN ($indexs)";
	        $criteria->limit = $limit;
	        $userArray = WuZiKu::model()->findAll($criteria);
	        $returnVal = '';
	        foreach($userArray as $userAccount)
	        {
	        	$returnVal .= $userAccount->getAttribute('name').'|'.$userAccount->getAttribute('uniqueId')."\n";
	        }
	        echo $returnVal;
       }
    }
    
}